// pages/compete/compete.js
import {
  fetchAuctionGoodsList,
  fetchGoodsList
} from '../../services/good/fetchGoods';
Page({
  data: {
    searchValue: '',
    active: '',
    secActive: 0,
    goodsList: [],
    tempGoodsList: [],
    tabs: [{
        value: '',
        label: '全部'
      },
      {
        value: 0,
        label: '即将开始'
      }, {
        value: 1,
        label: '正在进行'
      }, {
        value: 2,
        label: '已结束'
      }
    ],
    reviewStatus: {
      0: '即将开始',
      1: '正在进行',
      2: '已结束',
    },
    statusTypeMap: {
      0: 'warning',
      1: 'primary',
      2: 'default',
    },
    privateData: {
      tabIndex: 0,
    },
  },

  goodListPagination: {
    index: 1,
    num: 6,
  },

  onShow() {
    this.getTabBar().init();
    this.init();
  },

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage()
  },
  loadHomePage() {
    wx.stopPullDownRefresh();

    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },
  async loadGoodsList(fresh = false) {
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
    }

    this.setData({
      goodsListLoadStatus: 1
    });

    const pageSize = this.goodListPagination.num;
    let pageIndex = this.goodListPagination.index + 1;
    if (fresh) {
      pageIndex = 1;
    }

    try {
      const userInfo = wx.getStorageSync('userInfo')
      let isMy = false;
      if (this.data.secActive == '1') isMy = true
      let result = {}

      result = isMy && userInfo.userType !== 1 ? await fetchAuctionGoodsList(pageIndex, pageSize, this.data.searchValue, this.data.active, isMy ? userInfo.id : '') : await fetchGoodsList(pageIndex, pageSize, this.data.searchValue, this.data.active, isMy ? userInfo.id : '');
      const {
        records: nextList
      } = result
      console.log(result)
      this.setData({
        userInfo,
        goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
        goodsListLoadStatus: 0,
      });

      this.goodListPagination.index = pageIndex;
      this.goodListPagination.num = pageSize;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },
  onTabsClick(event) {
    this.setData({
      active: event.detail.value
    })

    this.loadGoodsList(true);
  },
  onSecTabsClick(event) {
    this.setData({
      secActive: event.detail.value,
    })
    this.loadGoodsList(true);
  },
  goDetail(event) {
    const target = event.currentTarget
    wx.navigateTo({
      url: `/pages/compete/detail/detail?id=${target.dataset.id}`,
    })
  },
  handleSubmit() {
    this.init();
  },
  onReTry() {
    this.loadGoodsList();
  },

})